//
//  JPHomeTableViewController.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 16/9/19.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit

private let JPHomeTableViewCellIndentifier = "JPHomeTableViewCellIndentifier"

//屏幕宽度
let screenWidth = UIScreen.main.bounds.size.width
//底部视图高度
let footerViewHeight = 35

class JPHomeTableViewController: JPVistorViewController {
    
    fileprivate lazy var statusListViewModel: JPStatusListViewModel = JPStatusListViewModel()
    
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
        let pullUpView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        pullUpView.activityIndicatorViewStyle = .gray
        
        return pullUpView
    }()
    
    private lazy var pullDownView: JPRefreshControl = {
        let pullDownView = JPRefreshControl()
        pullDownView.addTarget(self, action: #selector(pullDownRefreshData), for: .valueChanged)
        
        return pullDownView
    }()
    
    lazy var tipLabel: UILabel = {
        let tip = UILabel(textColor: UIColor.white, fontSize: 12)
        tip.backgroundColor = UIColor.orange
        tip.text = "没有加载到最新的微博数据"
        tip.textAlignment = .center
        tip.isHidden = true
        return tip
    }()
    
//    private lazy var pullDownView: UIRefreshControl = {
//        let pullDownView = UIRefreshControl()
//        pullDownView.addTarget(self, action: #selector(pullDownRefreshData), for: .valueChanged)
//        
//        return pullDownView
//    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            vistorView = JPVistorView(frame: UIScreen.main.bounds, title: nil, imageName: nil)
        }else {
            loadData()
            self.setupTableView()
        }
        
        setupUI()
    }
    //加载数据
    fileprivate func loadData() {
        
        statusListViewModel.loadData(isPullUp: pullUpView.isAnimating) { (isSuccess, message) in
            
            self.pullUpView.stopAnimating()
            self.pullDownView.endRefreshing()
            
            if isSuccess {
                self.tableView.reloadData()
                self.pullDownView.endRefreshing()
            }
            //  
            if !self.pullUpView.isAnimating {
                self.startTipAnimation(message: message)
            }
        }
    }
    
    //  设置UI
    private func setupUI() {
        
        if let nav = self.navigationController {
            //  swift3.0可以得到一个视图的最大Y值
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
    }
    
    private func startTipAnimation(message: String) {
        if tipLabel.isHidden == false {
            return
        }
        tipLabel.isHidden = false
        tipLabel.text = message
        UIView.animate(withDuration: 1, animations: { 
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: 35)
            }) { (_) in
                UIView.animate(withDuration: 1, animations: { 
                    self.tipLabel.transform = CGAffineTransform.identity
                    }, completion: { (_) in
                        self.tipLabel.isHidden = true
                })
        }
        
    }
    
    //设置tableView属性
    private func setupTableView() {
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        tableView.register(JPHomeTableViewCell.self, forCellReuseIdentifier: JPHomeTableViewCellIndentifier)
        tableView.tableFooterView = pullUpView
        tableView.showsVerticalScrollIndicator = false
        tableView.addSubview(pullDownView)
    }
    
    //下拉刷新数据
    @objc private func pullDownRefreshData() {
        loadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

//  MARK: -- UITableViewDataSource
extension JPHomeTableViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: JPHomeTableViewCellIndentifier, for: indexPath) as! JPHomeTableViewCell
        //  获取cell对应的ViewModel
        let viewModel: JPStatusViewModel = statusListViewModel.statusList[indexPath.row]
        //  绑定数据
        cell.statusViewModel = viewModel
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == statusListViewModel.statusList.count - 1 && !pullUpView.isAnimating {
            pullUpView.startAnimating()
            loadData()
        }
    }
}
